package com.aggeek.springbootlearning;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

/**
 * author:liuao
 * description:http请求
 * Date: create on 14:37 2017/12/11
 * modify by:
 */
public class HttpUtil {

     public static void main(String[] args){

     }

    /**
     * get请求demo
     * @param stringurl
     * @return
     */
     public static JSONObject get(String stringurl){
         StringBuilder sb = new StringBuilder();
         try {
             URL  url = new URL("http://www.aoaoshop.com/product/list.do?keyword=i&orderBy=price_desc");
             //打开连接
             HttpURLConnection connection=(HttpURLConnection)url.openConnection();
             /**
              * getContentType()返回响应主体的mime内容类型
              * getContentLength()返回内容中有多少个字节，或者getContentLengthLong()
              *
              *
              */
             //设置连接信息，请求头等
             connection.setRequestProperty("user-agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");

             connection.connect();
             BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
             String line;

             while ((line = bf.readLine())!=null){
                 sb.append(line);

             }
             bf.close();
             connection.disconnect();
         } catch (MalformedURLException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
         JSONObject jsonObject = JSONObject.parseObject(sb.toString());
         System.out.println(jsonObject);
         return  jsonObject;
     }
     public static JSONObject post(String url,Map<String,String> param) {
         PrintWriter out = null;
         BufferedReader in = null;
         String result = "";
         StringBuilder builder = new StringBuilder();
         try {
             URL realUrl = new URL(url);
             // 打开和URL之间的连接
             URLConnection conn = realUrl.openConnection();
             // 设置通用的请求属性
             conn.setRequestProperty("accept", "*/*");
             conn.setRequestProperty("connection", "Keep-Alive");
             conn.setRequestProperty("user-agent",
                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
             // 发送POST请求必须设置如下两行
             conn.setDoOutput(true);
             conn.setDoInput(true);
             // 获取URLConnection对象对应的输出流
             out = new PrintWriter(conn.getOutputStream());
             // 发送请求参数
             out.print(param);
             out.flush();
             in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
             while ((result = in.readLine()) != null) {
                 builder.append(result);
             }


         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             try {
                 if (out != null) {
                     out.close();
                 }
                 if (in != null) {
                     in.close();
                 }
             } catch (IOException ex) {
                 ex.printStackTrace();
             }
         }
         return JSONObject.parseObject(builder.toString());
     }
}
